<template>
  <view class="goods-item">
    <!-- 左侧的盒子 -->
    <view class="goods-item-left">
      <radio :checked="item.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"/>
      <image :src="item.goods_small_logo || defaultPic" class="goods-pic"></image>
    </view>
    <!-- 右侧的盒子 -->
    <view class="goods-item-right">
      <!-- 商品的名字 -->
      <view class="goods-name">{{item.goods_name}}</view>
      <view class="goods-info-box">
        <!-- 商品价格 -->
        <view class="goods-price">￥{{item.goods_price | tofixed}}</view>
        <!-- 商品数量 -->
        <uni-number-box :min="1" :max="99" :value="item.goods_count" v-if="showNumberBox" @change="numCahngeHandler"/>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:'my-goods',
    props: {
      item: {
        type: Object,
        default: function () {
          return {} //如果是对象就是return {}
        }
      },
      showRadio: {
        type: Boolean,
        default: false
      },
      showNumberBox: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return { 
        // 默认图片
        defaultPic: 'https://ts1.cn.mm.bing.net/th/id/R-C.f6b3a04a9145076738f43902749fdd3a?rik=4xiCuUlHXSFdnA&riu=http%3a%2f%2fpic.616pic.com%2fys_img%2f00%2f05%2f66%2fA6XaeFUerS.jpg&ehk=J3Cfx00exOIb1VHatGoeUOuqiFT%2fBzNhIoxdMumr7ko%3d&risl=&pid=ImgRaw&r=0',
        
      }
    },
    filters: {
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    methods: {
      // 这是radio 组件 点击的事件处理函数
      radioClickHandler() {
        this.$emit('radio-change', {
          goods_id: this.item.goods_id,
          goods_state: !this.item.goods_state
        })
      },
      // 监听到了 numberchange 事件的触发
      numCahngeHandler(val) {
        this.$emit('num-change', {
          goods_id: this.item.goods_id,
          goods_count: +val
        })
        // console.log(val)
      }

    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;
    .goods-item-left {
      margin-right: 5px;
      display: flex;
      align-items: center;
      
      .goods-pic {
        width: 100px;
        height: 100px;
        display: block;
      }
    }
    .goods-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;

      .goods-name {
        font-size: 13px;
      }
      .goods-info-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        
        .goods-price {
          color: #C00000;
          font-size: 16px;
        }
        
      }
    }
  }

</style>